package com.jxc.jxc01.grc.web;

import com.alibaba.fastjson.JSON;
import com.jxc.jxc01.grc.dao.IVendorDao;
import com.jxc.jxc01.grc.dao.impl.VendorDaoImpl;
import com.jxc.jxc01.grc.entity.RespResult;
import com.jxc.jxc01.grc.entity.Vendor;
import com.jxc.jxc01.grc.service.IVendorService;
import com.jxc.jxc01.grc.service.impl.VendorServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(value = "/queryVendor")
public class QueryVendorServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request,
                           HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("进入到queryVendor==》");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        RespResult result = (RespResult) request.getSession().getAttribute("result");
        IVendorService vendorService = new VendorServiceImpl();

        List<Vendor> vendors = vendorService.queryVendors();
        System.out.println(vendors);
        if (result == null) {
            if (vendors != null && vendors.size() > 0) {
                int count = vendorService.queryVendorsTotalCount();
                RespResult respResult = new RespResult();
                respResult.setData(vendors);
                respResult.setCode(0);
                respResult.setMessage("ok");
                respResult.setCount(count);
                System.out.println("进入到queryVendor==》" + respResult);
                writer.print(JSON.toJSONString(respResult));
            }
        }else {
        writer.print(JSON.toJSONString(result));
          request.getSession().invalidate();
        }
    }
}
